#import "XLJSHandler.h"
@interface XLJSHandler ()
@end
@implementation XLJSHandler
-(instancetype)initWithViewController:(UIViewController *)webVC configuration:(WKWebViewConfiguration *)configuration {
    self = [super init];
    if (self) {
        _webVC = webVC;
        _configuration = configuration;
        [configuration.userContentController addScriptMessageHandler:self name:@"copyText"];
        [configuration.userContentController addScriptMessageHandler:self name:@"backPage"];
    }
    return self;
}
#pragma mark -  JS 调用 Native  代理
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    if ([message.name isEqualToString:@"backPage"]) {
        if (self.webVC.presentingViewController) {
            [self.webVC dismissViewControllerAnimated:YES completion:nil];
        }else{
            [self.webVC.navigationController popViewControllerAnimated:YES];
        }
    }else if([message.name isEqualToString:@"copyText"])
    {
        [UIPasteboard generalPasteboard].string = message.body;
        [HUDHelper alert:ASLocalizedString(@"复制成功")];
    }
}
#pragma mark -  记得要移除
-(void)cancelHandler {
    [_configuration.userContentController removeScriptMessageHandlerForName:@"backPage"];
    [_configuration.userContentController removeScriptMessageHandlerForName:@"JavaScriptInterface.copyText"];
}
-(void)dealloc {
}
@end
